/*
 * Group.java
 *
 * Created on August 15, 2007, 5:37 PM
 *
 * To change this templat, choose Tools | Template Manager
 * and open the template in the editor.
 */

package timeitj.persistence;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.log4j.Logger;
import timeitj.ejb.SessionFacadeBean;

/**
 * Entity class Group
 * 
 * 
 * 
 * @author wallner
 */
@Entity
@NamedQuery(
    name="GroupByName",
    query="SELECT g FROM Group g WHERE g.groupName LIKE :name ")
            
            
@Table(name="GROUPTABLE")
public class Group implements Serializable {
    
    
    private static Logger logger = Logger.getLogger(Group.class.getName());

    @Id @GeneratedValue
    @Column(name="ID")
    private Long id;
    
    @Version
    @Column(name="OPT_LOCK")
    private Long version;
    
    @Column(name="GROUPNAME")
    private String groupName;
    
    @Column(name="MEMBERS")
    @ManyToMany(mappedBy = "groups")
    private List<Person> members;
    
    /**
     * Creates a new instance of Group
     */
    public Group() {
        members = new ArrayList();
    }

    /**
     * 
     * Create a new instance of Group with a given name
     */
    
    public Group(String name){
	this();
	this.groupName = name;
        logger.info("Group '" + name + "' created");
    }
    
    /**
     * Gets the id of this Group.
     * @return the id
     */
    public long getId() {
        return this.id;
    }
    
    /**
     *Get the name of the role
     *@return the name of the role as String
     */
    public String getGroupName() {
	return groupName; 
    } 
    
    /**
     * set the name of the role
     */
    public void setGroupName(String groupName) { 
        this.groupName = groupName; 
    }
    
    /**
     * Returns a java.util.List of all members of that role
     * @return a java.util.List of Persons
     */
    public List<Person> getMembers(){
        return this.members;
    }
    
    /**
     * Adds a given Person to that Role
     */
    public void addPerson(Person person){
        members.add(person);
        logger.info("Person '" + person.getLogin() +"' added to Group '" + groupName +"'");
    }
    
    /**
     * Returns a hash code value for the object.  This implementation computes 
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += id.intValue();
        return hash;
    }

    /**
     * Determines whether another object is equal to this Group.  The result is 
     * <code>true</code> if and only if the argument is not null and is a Group object that 
     * has the same id field values as this object.
     * 
     * 
     * 
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Group)) {
            return false;
        }
        Group other = (Group)object;
        if (this.id != other.id) return false;
        return true;
    }

    /**
     * Returns a string representation of the object.  This implementation constructs 
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "timeitj.persistence.Group[id=" + id + "]";
    }

}
